
import 'dart:collection';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';
import 'package:xsdk_flutter_plugin/xsdk_flutter_plugin.dart';
enum TtsState { playing, stopped, paused, continued }

/*
测试Realm数据库
 */
class TestAndroidStatePage extends XPageState{

  String? imgUrl;

  TestAndroidStatePage({
    this.imgUrl
  });


  @override
  void onStart() {
    Map<String,Object> map={};
    map['appId']='2021004169629104';
    map['pId']='2088611935241475';
    XFlutterPluginUtils.initAlipayIotSDK(XJsonUtils.obj2Json(map));

    XFlutterPluginUtils.setReceiveNativeMessageCallback((resultMap){
      XLogUtils.printLog('---XFlutterPluginUtils.setReceiveNativeMessageCallback=$resultMap');

      String type=resultMap['type'];
      if('AlipayIot_init'==type){
        String dataJson=resultMap['data'];
        Map<String,dynamic> dataJsonMap=XJsonUtils.json2Map(dataJson);
        String code=dataJsonMap['code'];
        if('1000'==code){
          XLogUtils.printLog('---AlipayIot_init,初始化成功');
        }else{
          XLogUtils.printLog('---AlipayIot_init,初始化失败');
        }
      }else if('AlipayIot_memberCard'==type){
        //会员开卡/登录
        String dataJson=resultMap['data'];
        Map<String,dynamic> dataJsonMap=XJsonUtils.json2Map(dataJson);
        String code=dataJsonMap['code'];
        if('1000'==code){
          XLogUtils.printLog('---AlipayIot_memberCard,code=1000');
        }else{
          XLogUtils.printLog('---AlipayIot_memberCard,code!=1000');
        }
      }else if('AlipayIot_memberPay'==type){
        //会员开卡/登录
        String dataJson=resultMap['data'];
        Map<String,dynamic> dataJsonMap=XJsonUtils.json2Map(dataJson);
        String code=dataJsonMap['code'];
        if('1000'==code){
          XLogUtils.printLog('---AlipayIot_memberPay,code=1000');
        }else{
          XLogUtils.printLog('---AlipayIot_memberPay,code!=1000');
        }
      }else if('AlipayIot_commonPay'==type){
        //会员开卡/登录
        String dataJson=resultMap['data'];
        Map<String,dynamic> dataJsonMap=XJsonUtils.json2Map(dataJson);
        String code=dataJsonMap['code'];
        if('1000'==code){
          XLogUtils.printLog('---AlipayIot_commonPay,code=1000');
        }else{
          XLogUtils.printLog('---AlipayIot_commonPay,code!=1000');
        }
      }

    });

  }

  @override
  bool isDoubleClickToQuit() {
    return false;
  }

  //methodChannel2.invokeMethod('flutter_test_1','55555');
  static const methodChannel2 = MethodChannel('xsdk_channel');

  @override
  Widget getBodyWidget(BuildContext context) {

    if(imgUrl!=null){
      return XWidgetUtils.getLayoutContainer(
          child: XImageUtils.getImageWidgetWithUrl(
              width: 200, height: 220, imageUrl: imgUrl??'', defaultImageAssetName: 'index_no_data_5.png')
      );
    }

    return  SingleChildScrollView(
      scrollDirection: Axis.vertical,
      child: Column(
        children: [
          XWidgetUtils.getWidgetTextField(hintText: '请输入'),
          XWidgetUtils.getWidgetButtonText(text: '初始化商米sdk', onPressed: (){
            XFlutterPluginUtils.initShangMiSDK();
          }),
          XWidgetUtils.getWidgetButtonText(text: '商米机打印测试', onPressed: (){
            XFlutterPluginUtils.printMsgByShangMi('saljfsa\n');
          }),
          XWidgetUtils.getWidgetButtonText(text: '权限', onPressed: (){
            XPermissionUtils.requestAllPermissions();
          }),

          XWidgetUtils.getWidgetButtonText(text: 'usb serial', onPressed: (){
            
            XSerialApi serX=XSerialApi(callback: (Uint8List? re){
              
            });
            serX.connect('/dev/ttyS4',serialPortMode:3);

          }),
          XWidgetUtils.getWidgetButtonText(text: 'serial port', onPressed: () async {
            // serialApi?.connect('/dev/ttyHSL1');
            Map<String, dynamic> deviceInfo= await XFlutterUtils.getDeviceInfo();
            String brand=deviceInfo['brand'];
            String model=deviceInfo['model'];
            XLogUtils.printLog("---设备brand:$brand,model:$model");//SUNMI

            XFlutterPluginUtils.serialPortConnect('ZhiQ-R68P');
            //接收原生通知 {"type":"weigher","data":"0.295"}
            XFlutterPluginUtils.setReceiveNativeMessageCallback((resultMap) {
              XLogUtils.printLog("---电子秤json=$resultMap");
            });

          }),
          XWidgetUtils.getWidgetButtonText(text: '发送语音', onPressed: (){
            XTTSUtils.speak('A12345');
          }),
          XWidgetUtils.getWidgetButtonText(text: '识别图片', onPressed: (){
            XFlutterPluginUtils.goBackToScreen();
          }),
          XWidgetUtils.getWidgetButtonText(text: '分屏', onPressed: () async {
            String url='http://www.baidu.com';
            XFlutterPluginUtils.openSecondScreen(url);
          }),
          XWidgetUtils.getWidgetButtonText(text: '支付宝碰一碰支付', onPressed: (){
            //
            Map<String,Object> map={};
            map['operateType']='show';
            map['amount']='1.05';
            map['tradeNo']='100001001000';
            XFlutterPluginUtils.commonPayAlipayIotSDK(XJsonUtils.obj2Json(map));
          }),
          XWidgetUtils.getWidgetButtonText(text: '支付宝碰一碰会员支付', onPressed: (){
            //
            Map<String,Object> map={};
            map['operateType']='show';
            map['itemAppId']='2021002190623737';
            map['templateId']='20240830000000005708166000300470';
            XFlutterPluginUtils.memberPayAlipayIotSDK(XJsonUtils.obj2Json(map));
          }),
          XWidgetUtils.getWidgetButtonText(text: '支付宝碰一碰会员登录', onPressed: (){
            //
            Map<String,Object> map={};
            map['operateType']='show';
            map['itemAppId']='2021002190623737';
            map['templateId']='20240830000000005708166000300470';
            XFlutterPluginUtils.memberCardAlipayIotSDK(XJsonUtils.obj2Json(map));
          }),
          XWidgetUtils.getWidgetText('$deviceInfo'),
        ],
      ),
    );
  }




  String? deviceInfo;

  @override
  Future<void> updateData({int pageIndex=1}) async {

    Map<String, dynamic> re1= await XFlutterUtils.getDeviceInfo();

    setState(() {
      deviceInfo=XJsonUtils.obj2Json(re1);
    });

  }





}

